Skip to main content

What is CoCa?

CoCa (Contrastive Captioner) is an extension of CLIP that combines:
  1. Contrastive Learning: Standard CLIP image-text matching
  2. Generative Captioning: Auto-regressive caption generation
This dual objective enables CoCa models to:
  • Perform zero-shot image classification (like CLIP)
  • Generate natural language captions for images
  • Achieve better representations through the combined training signal
Paper: CoCa: Contrastive Captioners are Image-Text Foundation Models

CoCa Architecture

CoCa adds a multimodal text decoder on top of the standard CLIP architecture:
Key components:
  • Image Encoder: Same as CLIP (ViT, ResNet, etc.)
  • Unimodal Text Encoder: Encodes text for contrastive learning
  • Multimodal Text Decoder: Cross-attends to image features to generate captions

Available CoCa Models

OpenCLIP provides several CoCa model configurations:

Model Configs

Multimodal Decoder Configuration

Example configuration from coca_ViT-B-32:

Training CoCa from Scratch

Basic CoCa Training

Train CoCa with both contrastive and captioning objectives:
Loss weights:
  • --coca-contrastive-loss-weight 1.0: Weight for CLIP contrastive loss
  • --coca-caption-loss-weight 2.0: Weight for caption generation loss

Multi-GPU CoCa Training

Fine-tuning CoCa

Fine-tuning on MSCOCO Captions

OpenCLIP provides a pretrained CoCa model that can be fine-tuned for captioning:
Key changes for fine-tuning:
  • --pretrained laion2b_s13b_b90k: Start from pretrained weights
  • --lr 1e-5: Lower learning rate for fine-tuning
  • --epochs 1: Fine-tune for fewer epochs
  • --coca-contrastive-loss-weight 0: Disable contrastive loss (captioning only)
  • --coca-caption-loss-weight 1: Only train the generative head

Preparing MSCOCO Data

Create a CSV file with image paths and captions using CLIP_benchmark:
This creates a tab-separated CSV:

Generating Captions with CoCa

Basic Caption Generation

Batch Caption Generation

Advanced Generation Options

CoCa vs CLIP

When to Use CoCa

Use CoCa when:
  • You need both contrastive and generative capabilities
  • Image captioning is important for your application
  • You want richer image-text representations
  • You have data with detailed captions

When to Use CLIP

Use CLIP when:
  • You only need contrastive learning (classification, retrieval)
  • Training speed is critical (CoCa is slower due to caption generation)
  • You have limited compute resources
  • Your captions are short or simple

Training Time Comparison

CoCa is slower due to the autoregressive caption generation during training.

Example Training Configurations

Small-Scale CoCa Training

Large-Scale CoCa Training

CoCa with RoBERTa Text Encoder

Pretrained CoCa Models

OpenCLIP provides pretrained CoCa models:
Available pretrained weights:
  • laion2b_s13b_b90k: Pretrained on LAION-2B
  • mscoco_finetuned_laion2B-s13B-b90k: LAION-2B pretraining + MSCOCO fine-tuning

Using CoCa for Multiple Tasks

Image Classification (Zero-Shot)

Image Captioning

Image-Text Retrieval

Tips for Training CoCa

Training tips:
  • Start with contrastive-only training, then add caption loss gradually
  • Use higher weight for caption loss (2.0 vs 1.0 for contrastive)
  • Fine-tune on high-quality caption datasets (MSCOCO) for best generation
  • Use gradient checkpointing for memory efficiency with large models
  • Caption generation is slower - expect 40-60% of CLIP training speed
Common issues:
  • CoCa requires more memory due to the multimodal decoder
  • Gradient accumulation is not compatible with distillation for CoCa
  • Caption quality depends heavily on training data quality
  • Very short captions may not benefit from the generative objective

Credits

CoCa implementation in OpenCLIP:

Next Steps

Training Overview

Learn about general CLIP training

Fine-tuning

Fine-tune CoCa models on custom datasets

Configuration

Explore all CoCa training parameters

Inference

Use pretrained CoCa for captioning and classification